//---------------------------------------------------------------------------------
// Copyright (c) 2011, Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//---------------------------------------------------------------------------------

namespace Microsoft.ServiceBus.Samples
{
    using System;
    using Microsoft.ServiceBus;
    using Microsoft.ServiceBus.Messaging;

    public class Sender
    {
        private static string serviceNamespace, issuerName, issuerSecret;

        public static void Main()
        {
            // Get credentials and set up management and runtime messaging entities:

            GetUserCredentials();
            NamespaceManager namespaceClient = CreateNamespaceManager();
            MessagingFactory messagingFactory = CreateMessagingFactory();

            Console.WriteLine("Creating queue 'OrdersService'...");

            if(namespaceClient.QueueExists("OrdersService"))
                namespaceClient.DeleteQueue("OrdersService");

            QueueDescription queue = namespaceClient.CreateQueue("OrdersService");

            QueueClient queueClient = messagingFactory.CreateQueueClient(queue.Path, ReceiveMode.ReceiveAndDelete);
            
            // Send messages to queue, of different order types:
            Console.WriteLine("Sending messages to queue...");
                CreateAndSendOrderMessage("DeliveryOrder", 1, 10, 15, queueClient);
                CreateAndSendOrderMessage("StayInOrder", 2, 15, 500, queueClient);
                CreateAndSendOrderMessage("TakeOutOrder", 3, 1, 25, queueClient);
                CreateAndSendOrderMessage("TakeOutOrder", 5, 3, 25, queueClient);
                CreateAndSendOrderMessage("DeliveryOrder", 4, 100, 100000, queueClient);
            

            Console.WriteLine();
            Console.WriteLine("Press [Enter] to delete queue and exit.");
            Console.ReadLine();

            // Cleanup:
            messagingFactory.Close();
            namespaceClient.DeleteQueue(queue.Path);
        }

        private static void GetUserCredentials()
        {
            // User namespace
            Console.WriteLine("Please provide the namespace to use:");
            Sender.serviceNamespace = Console.ReadLine().Trim();

            // Issuer name
            Console.WriteLine("Please provide the Issuer name to use:");
            Sender.issuerName = Console.ReadLine().Trim();

            // Issuer key
            Console.WriteLine("Please provide the Issuer key to use:");
            Sender.issuerSecret = Console.ReadLine().Trim();
        }

        private static NamespaceManager CreateNamespaceManager()
        {
            Uri managementAddress = ServiceBusEnvironment.CreateServiceUri("sb", Sender.serviceNamespace, string.Empty);

            return new NamespaceManager(
                managementAddress,
                TokenProvider.CreateSharedSecretTokenProvider(Sender.issuerName, Sender.issuerSecret));
        }

        private static MessagingFactory CreateMessagingFactory()
        {
            Uri runtimeAddress = ServiceBusEnvironment.CreateServiceUri("sb", Sender.serviceNamespace, string.Empty);

            return MessagingFactory.Create(
                runtimeAddress,
                TokenProvider.CreateSharedSecretTokenProvider(Sender.issuerName, Sender.issuerSecret));
        }

        private static void CreateAndSendOrderMessage(string orderType, int? orderNumber, int numberOfItems, int orderTotal, QueueClient sender)
        {
            var message = new BrokeredMessage(Guid.NewGuid().ToString());
            message.Properties.Add("OrderType", orderType);
            message.Properties.Add("OrderNumber", orderNumber);
            message.Properties.Add("NumberOfItems", numberOfItems);
            message.Properties.Add("OrderTotal", orderTotal);

            Console.WriteLine("Sending message of order type {0}.", message.Properties["OrderType"]);
            sender.Send(message);
        }
    }
}
